package com.authorityManagement.controller;

import com.authorityManagement.entity.LoginUser;
import com.authorityManagement.entity.Menu;
import com.authorityManagement.service.RouterService;
import com.authorityManagement.util.resultUtil.R;
import com.authorityManagement.vo.RouterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/router")
public class RouterController {
      
      @Autowired
      private RouterService routerService;
      
      @GetMapping("/queryRouterMenuList")
      public R queryRouterMenuList(){
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
            List<Menu> menuList = loginUser.getMenuList();
            List<Menu> collect = null;
            if (!CollectionUtils.isEmpty(menuList)) {
                  collect = menuList.stream()
                        .filter(item -> item != null && item.getType() != 2)
                        .collect(Collectors.toList());
            }
            List<RouterVo> routerVoList = routerService.queryRouterMenuList(collect,0L);
            return R.ok().data("routerList", routerVoList);
      }
      
      @GetMapping("/queryMenuList")
      public R queryMenuList(){
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            LoginUser loginUser = (LoginUser) authentication.getPrincipal();
            List<Menu> menuList = loginUser.getMenuList();
            List<RouterVo> routerVoList = routerService.queryRouterMenuList(menuList,0L);
            return R.ok().data("routerList", routerVoList);
      }
      
      
}
